function get_work_hours() {
    let area = wps.Range("e3:ai300");
    var total_work_hours = 0;
    var total_work_over_hours = 0;
    var total_all_hours = 0;
    for (var i = 1; i <= area.Rows.Count; i++) {
        if (!wps.Range(`b${i + 3}`).Text) {
            break;
        }
        total_work_hours = 0;
        total_work_over_hours = 0;
        total_all_hours = 0;
        for (var j = 1; j <= area.Columns.Count; j++) {
            //星期标识
            var week_day = area.Cells.Item(1, j).Text;
            console.log(week_day);
            var on_duty = '';
            var is_week_day = false;
            var is_friday = false;
            //上班时间
            var duty = area.Cells.Item(i + 1, j).Text; //上班，下班时间在同一个单元格中
            if (duty) {
                duty = duty.replace(/[^0-9:]/ig, '');
                /*
                如果是正常的工作日（有些特殊情况，比如周末是假期调休的情况，我们就不考虑了）
                统一都是按09:00上班
                如果是周末就按打开的时间进行计算
                */
                if (week_day.indexOf('六') > -1 || week_day.indexOf('天') > -1) {
                    on_duty = duty.substring(0, 5);
                    is_week_day = true;
                } else {
                    on_duty = "9:00";
                }
                if (week_day.indexOf('五') > -1) {//星期五特殊，因为可能存在当前只有上班记录，没有下班记录的情况，有可能是通宵发版
                    is_friday = true;
                }
                //如果是周五，还需要考虑是不是通宵的时间
                if (is_friday) {
                    var next_day_duty = area.Cells.Item(i + 1, j + 1).Text; //下一天的打卡时间
                    if (!next_day_duty && duty.length === 5) {
                        continue;
                    }

                    next_day_duty = next_day_duty.replace(/[^0-9:]/ig, '');
                    var next_day_on_duty = next_day_duty.substring(0, 5);
                    var next_day_off_duty = next_day_duty.slice(-5);
                    var next_day_time_diff = getTimeDiffInHours(next_day_on_duty, next_day_off_duty);
                    if (next_day_time_diff <= 3) {//说明是通宵
                        //1. 出勤
                        let work_hours = 8;

                        //2. 加班时长
                        let work_over_hours = 5.5 + getTimeDiffInHours("00:00", next_day_off_duty);

                        //3. 工时 = 出勤+加班时长
                        let total_hours = work_hours + work_over_hours;

                        //计算某一个人一天的和
                        total_work_hours = total_work_hours + work_hours;
                        total_work_over_hours = total_work_over_hours + work_over_hours;
                        total_all_hours = total_all_hours + total_hours;
                        continue;
                    }
                }


                //如果是周末，还需要考虑是不是通宵的时间
                var off_duty = duty.slice(-5);
                var time_diff = getTimeDiffInHours(on_duty, off_duty);
                if (is_week_day && time_diff <= 3) { //说明是通宵
                    //需要考虑是前一天通宵的情况，周五需要重新计算
                    // var pre_duty = area.Cells.Item(i + 1, j - 1).Text; //上班，下班时间在同一个单元格中
                    // if (!pre_duty) {
                    //     continue;
                    // }
                    // pre_duty = pre_duty.replace(/[^0-9:]/ig, '');
                    // var pre_on_duty = pre_duty.substring(0, 5);
                    // var pre_off_duty = duty.slice(-5);
                    // var pre_time_diff = getTimeDiffInHours(pre_on_duty, pre_off_duty);
                    // let pre_work_over_hours = Math.floor(pre_time_diff - 9.5);//这个需要减掉，然后重新计算，用第二天的打开时间
                    // if (pre_work_over_hours <= 0) {
                    //     pre_work_over_hours = 0;
                    // }

                    // var new_time_diff = getTimeDiffInHours(pre_on_duty, off_duty, true);//用第二天的时间减去
                    // //2. 加班时长

                    // let work_over_hours = Math.floor(new_time_diff - 9.5);
                    // if (work_over_hours <= 0) {
                    //     work_over_hours = 0;
                    // }
                    // total_work_over_hours = total_work_over_hours + (work_over_hours - pre_work_over_hours);
                    // total_all_hours = total_all_hours + work_over_hours - pre_work_over_hours;

                    continue;

                }

                //1. 出勤
                let work_hours = 8;//Math.floor(getTimeDiffInHours(on_duty, off_duty)-1);
                if (off_duty >= '19:00' && off_duty <= "19:30") {
                    work_hours = work_hours + 0.5
                }

                //2. 加班时长

                let work_over_hours = time_diff - 9.5;
                if (work_over_hours <= 0) {
                    work_over_hours = 0;
                }

                //3. 工时 = 出勤+加班时长
                let total_hours = work_hours + work_over_hours;

                //计算某一个人一天的和
                total_work_hours = total_work_hours + work_hours;
                total_work_over_hours = total_work_over_hours + work_over_hours;
                total_all_hours = total_all_hours + total_hours;
            }

        }
        //设置一个月的统计结果
        if (total_work_hours > 0 || total_work_over_hours > 0 || total_all_hours > 0) {
            wps.Range(`aj${i + 3}`).Value2 = total_work_hours;
            wps.Range(`ak${i + 3}`).Value2 = total_work_over_hours;
            wps.Range(`al${i + 3}`).Value2 = total_all_hours;
        }
    }
}


function get_work_hours_new() {
    add_rows();
    let area = wps.Range("e3:ai600");
    var total_work_hours = 0;
    var total_work_over_hours = 0;
    var total_all_hours = 0;
    var name_row_index = 0;
    for (var i = 1; ; i = i + 4) {
        if (!wps.Range(`b${name_row_index + 4}`).Text) {//如果姓名为空，说明后面没有数据了
            break;
        }
        name_row_index = name_row_index + 4;
        total_work_hours = 0;
        total_work_over_hours = 0;
        total_all_hours = 0;
        for (var j = 1; j <= area.Columns.Count; j++) {
            //星期标识
            var week_day = area.Cells.Item(1, j).Text;
            var on_duty = '';
            var is_week_day = false;
            var is_friday = false;
            //上班时间
            var duty = area.Cells.Item(i + 1, j).Text; //上班，下班时间在同一个单元格中
            if (duty) {
                duty = duty.replace(/[^0-9:]/ig, '');
                /*
                如果是正常的工作日（有些特殊情况，比如周末是假期调休的情况，我们就不考虑了）
                统一都是按09:00上班
                如果是周末就按打开的时间进行计算
                */
                if (week_day.indexOf('六') > -1 || week_day.indexOf('天') > -1) {
                    on_duty = duty.substring(0, 5);
                    is_week_day = true;
                } else {
                    on_duty = "9:00";
                }

                if (week_day.indexOf('五') > -1) {//星期五特殊，因为可能存在当前只有上班记录，没有下班记录的情况，有可能是通宵发版
                    is_friday = true;
                }


                 //如果是周五，还需要考虑是不是通宵的时间
                 if (is_friday) {
                    var next_day_duty = area.Cells.Item(i + 1, j + 1).Text; //下一天的打卡时间
                    if (!next_day_duty && duty.length === 5) {
                        continue;
                    }

                    next_day_duty = next_day_duty.replace(/[^0-9:]/ig, '');
                    var next_day_on_duty = next_day_duty.substring(0, 5);
                    var next_day_off_duty = next_day_duty.slice(-5);
                    var next_day_time_diff = getTimeDiffInHours(next_day_on_duty, next_day_off_duty);
                    if (next_day_time_diff <= 3 && next_day_duty) {//说明是通宵
                        //1. 出勤
                        let work_hours = 8;
                        area.Cells.Item(i + 2, j).Value2 = work_hours;

                        //2. 加班时长
                        let work_over_hours = 5.5 + getTimeDiffInHours("00:00", next_day_off_duty);
                        area.Cells.Item(i + 3, j).Value2 = work_over_hours;

                        //3. 工时 = 出勤+加班时长
                        let total_hours = work_hours + work_over_hours;
                        area.Cells.Item(i + 4, j).Value2 = total_hours;

                        //计算某一个人一天的和
                        total_work_hours = total_work_hours + work_hours;
                        total_work_over_hours = total_work_over_hours + work_over_hours;
                        total_all_hours = total_all_hours + total_hours;
                        continue;
                    }
                }

                //如果是周末，还需要考虑是不是通宵的时间
                var off_duty = duty.slice(-5);
                var time_diff = getTimeDiffInHours(on_duty, off_duty);
                if (is_week_day && time_diff <= 3) { //说明是通宵
                    //需要考虑是前一天通宵的情况，周五需要重新计算
                    // var pre_duty = area.Cells.Item(i + 1, j - 1).Text; //上班，下班时间在同一个单元格中
                    // if (!pre_duty) {
                    //     continue;
                    // }
                    // pre_duty = pre_duty.replace(/[^0-9:]/ig, '');
                    // var pre_on_duty = pre_duty.substring(0, 5);
                    // var pre_off_duty = duty.slice(-5);
                    // var pre_time_diff = getTimeDiffInHours(pre_on_duty, pre_off_duty);
                    // let pre_work_over_hours = Math.floor(pre_time_diff - 9.5);//这个需要减掉，然后重新计算，用第二天的打开时间
                    // if (pre_work_over_hours <= 0) {
                    //     pre_work_over_hours = 0;
                    // }

                    // var new_time_diff = getTimeDiffInHours(pre_on_duty, off_duty, true);//用第二天的时间减去
                    // //2. 加班时长(重新计算)
                    // let new_work_over_hours = Math.floor(new_time_diff - 9.5);
                    // if (new_work_over_hours <= 0) {
                    //     new_work_over_hours = 0;
                    // }

                    // total_work_over_hours = total_work_over_hours + (new_work_over_hours - pre_work_over_hours);
                    // area.Cells.Item(i + 2, j - 1).Value2 = new_work_over_hours;
                    // total_all_hours = total_all_hours + new_work_over_hours - pre_work_over_hours;
                    // area.Cells.Item(i + 3, j - 1).Value2 = area.Cells.Item(i + 1, j - 1).Value2 + new_work_over_hours;
                    continue;

                }


                //1. 出勤
                let work_hours = 8;//Math.floor(getTimeDiffInHours(on_duty, off_duty)-1);
                if (off_duty >= '19:00' && off_duty <= "19:30") {
                    work_hours = work_hours + 0.5
                }
                area.Cells.Item(i + 2, j).Value2 = work_hours;

                //2. 加班时长
                let work_over_hours = time_diff - 9.5;
                if (work_over_hours <= 0) {
                    work_over_hours = 0;
                }
                area.Cells.Item(i + 3, j).Value2 = work_over_hours;

                //3. 工时 = 出勤+加班时长
                let total_hours = work_hours + work_over_hours;
                area.Cells.Item(i + 4, j).Value2 = total_hours;

                //计算某一个人一天的和
                total_work_hours = total_work_hours + work_hours;
                total_work_over_hours = total_work_over_hours + work_over_hours;
                total_all_hours = total_all_hours + total_hours;
            }

        }
        //设置一个月的统计结果
        if (total_work_hours > 0 || total_work_over_hours > 0 || total_all_hours > 0) {
            wps.Range(`aj${i + 4}`).Value2 = total_work_hours;
            wps.Range(`aj${i + 5}`).Value2 = total_work_over_hours;
            wps.Range(`aj${i + 6}`).Value2 = total_all_hours;
        }
    }
}

function getTimeDiffInHours(time1, time2, is_second_day = false) {
    // 将时间字符串转换为Date对象
    const date1 = new Date(`1970-01-01 ${time1}`);
    var date2;
    if (is_second_day) {
        date2 = new Date(`1970-01-02 ${time2}`);
    } else {
        date2 = new Date(`1970-01-01 ${time2}`);
    }

    // 计算时间差（毫秒）
    const timeDiff = Math.abs(date2 - date1);

    // 将时间差转换为小时
    const hoursDiff = timeDiff / 3600000;

    return Number.parseFloat(hoursDiff.toFixed(1));
}

function add_rows() {
    var sh = wps.ActiveSheet;
    if (!sh.Cells.Item(5, 4).Text === "出勤") {
        // alert("已经加过行了，你还想干嘛！");
        return;
    }
    for (let index = 5; ; index = index + 4) {
        if (!sh.Cells.Item(index, 2).Text) {
            break;
        }
        sh.Rows.Item(`${index}:${index}`).Select();//选中第一行
        wps.Selection.Insert(); //在选中行前面插入一行
        wps.Selection.Insert(); //在选中行前面插入一行
        wps.Selection.Insert(); //在选中行前面插入一行
        sh.Cells.Item(index, 4).Value2 = "出勤"
        sh.Cells.Item(index + 1, 4).Value2 = "加班"
        sh.Cells.Item(index + 2, 4).Value2 = "工时"
    }
    // sh.Rows.Item("1:1").Select();//选中第一行
    //sh.Rows.Item("1:1").Delete(); //删除第一行
    // wps.Selection.Insert(); //在选中行前面插入一行
}


export default {
    get_work_hours,
    add_rows,
    get_work_hours_new
}